require("../css/main.pcss");
require("../../../lib/js/lib/vhannels/vhannels.tool")
require('./aout.blog');

let Aout_blog = new window.Aout_blog('main');

/** 数据错误处理回调生成
 *
 * @param {Aout_blog} Aout_blog 博客数据对象
 *
 * @return {function}
 */
window.error = (Aout_blog) => {
    return () => {
        let dom = document.createElement("div");
        dom.innerHTML = `
            <section class="grid-y shadow alert callout">
                <div class="cell text-center">
                    <h3>没查到数据</h3>
                </div>
                <div class="cell titleimg grid-x align-center">
                    <img class="cell" src="" alt=""/>
                </div>
            </section>`;
        Aout_blog.data('root').before(dom.children[0]);
    }
};

// 分页相关
require('./pageindex');
// 内容获取
require('./conts');

(() => {
    Aout_blog.conts.paging(2, plushpagedata(Aout_blog), plushpageedge(Aout_blog))
        .conts(plushcont(Aout_blog), void 0, error(Aout_blog))
        .tags((tag) => {
            let root = Aout_blog.data("tags");
            let html =
                `<div class="cell shrink">
                    <a href="?tag=${tag}">${tag}</a>
                </div>`;

            root.forEach(v => v.innerHTML += html);
        }, undefined, () => {
            let root = Aout_blog.data("tags");
            let i = 0;
            for (const v of root) {
                if (i >= 5) return;
                ++i;
                v.innerHTML += `<div class="cell shrink">
                    <p>null 获取失败</p>
                </div>`;
            }
        });
})();

// 标签页面
require('./tags');

(() => {
    Aout_blog.tags.tags(showtag(Aout_blog), void 0, error(Aout_blog));
    Aout_blog.tags.tagconts(plushcont(Aout_blog), void 0, error(Aout_blog));
})();

window.addEventListener("load", () => {
    // 临时节点
    Aout_blog.data('dom', document.createElement("div"));
    // 数据根对象
    Aout_blog.data('root', document.querySelector("#contlist > div nav"));
    Aout_blog.data("tags", document.querySelectorAll(".tags"));

    // tag 参数数据
    let tag = vhannels.tool.getQueryString("tag");

    if (tag !== undefined) {
        document.querySelectorAll(".tags").forEach(v => v.remove());

        if (tag !== '')
            Aout_blog.tags.tagpaging(2, plushpagedata(Aout_blog, `tag=${tag}&`), plushpageedge(Aout_blog, `tag=${tag}&`));
        Aout_blog.tags.showtag(tag);

    } else Aout_blog.conts.run();
});